﻿#define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//初始化棋盘
void Init_Board(char arr[ROWS][COLS], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			arr[i][j] = set;
		}
	}
}

//打印棋盘
void Display_Board(char arr[ROWS][COLS], int row, int col)
{
	printf("--------扫雷游戏-------\n");
	for (int i = 0; i <= col; i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++) {
		printf("%d ", i);
		for (int j = 1; j <= col; j++) {
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

//布置10个雷
 //⽣成随机的坐标，布置雷
void Set_Mine(char arr[ROWS][COLS], int row, int col)
{
	int count = Easy_Count;
	while (count) {
		int x = rand() % row + 1;
		int y = rand() % col + 1;

		if (arr[x][y] == '0') {
			arr[x][y] = '1';
			count--;
		}
	}
}

int Get_Mine_Count(char arr[ROWS][COLS], int x, int y)
{
	int m = 0;
	for (int i = x - 1; i <= x + 1; i++) {
		for (int j = y - 1; j <= y + 1; j++) {
			m += arr[i][j];
		}
	}
	return m-9*'0';
}

//扫雷
void Find_Mine(char arr1[ROWS][COLS], char arr2[ROWS][COLS], int row, int col)
{
	printf("------开始扫雷------");
	int x=0, y=0;
	int win = 0;
	while (win < ROW * COL - Easy_Count) {
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= ROW && y >= 1 && y <= COL) {
			if (arr1[x][y] == '0') {
			//该位置不是雷，就统计这个坐标周围有⼏个雷
				int count = Get_Mine_Count(arr1, x, y);
				arr2[x][y] = count + '0';
				Display_Board(arr2, ROW, COL);
				win++;
			}
			else {
				printf("很遗憾，你被炸死了\n");
				Display_Board(arr1, ROW, COL);
				break;
			}
		}
		else {
			printf("坐标非法，重新输入\n");
		}
	}
	if (win == ROW * COL - Easy_Count) {
		printf("恭喜你，排雷成功\n");
		Display_Board(arr1, ROW, COL);
	}
}